using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UndergroundSurvival.Managers;

namespace UndergroundSurvival.UI
{
    public class UIManager : MonoBehaviour
    {
        public static UIManager Instance { get; private set; }

        [Header("Status Bars")]
        [SerializeField] private Slider hungerBar;
        [SerializeField] private Slider sleepBar;
        [SerializeField] private Slider mentalBar;
        [SerializeField] private Slider infectionBar;
        [SerializeField] private Slider temperatureBar;

        [Header("Status Text")]
        [SerializeField] private TextMeshProUGUI timeText;
        [SerializeField] private TextMeshProUGUI temperatureText;
        [SerializeField] private TextMeshProUGUI disasterWarningText;

        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
        }

        private void Start()
        {
            SubscribeToEvents();
        }

        private void SubscribeToEvents()
        {
            if (PlayerStatsManager.Instance != null)
            {
                PlayerStatsManager.Instance.OnHungerChanged += UpdateHungerBar;
                PlayerStatsManager.Instance.OnSleepChanged += UpdateSleepBar;
                PlayerStatsManager.Instance.OnMentalChanged += UpdateMentalBar;
                PlayerStatsManager.Instance.OnInfectionChanged += UpdateInfectionBar;
                PlayerStatsManager.Instance.OnTemperatureChanged += UpdateTemperatureBar;
            }
        }

        private void OnDestroy()
        {
            if (PlayerStatsManager.Instance != null)
            {
                PlayerStatsManager.Instance.OnHungerChanged -= UpdateHungerBar;
                PlayerStatsManager.Instance.OnSleepChanged -= UpdateSleepBar;
                PlayerStatsManager.Instance.OnMentalChanged -= UpdateMentalBar;
                PlayerStatsManager.Instance.OnInfectionChanged -= UpdateInfectionBar;
                PlayerStatsManager.Instance.OnTemperatureChanged -= UpdateTemperatureBar;
            }
        }

        private void UpdateHungerBar(float value)
        {
            if (hungerBar != null)
            {
                hungerBar.value = value;
            }
        }

        private void UpdateSleepBar(float value)
        {
            if (sleepBar != null)
            {
                sleepBar.value = value;
            }
        }

        private void UpdateMentalBar(float value)
        {
            if (mentalBar != null)
            {
                mentalBar.value = value;
            }
        }

        private void UpdateInfectionBar(float value)
        {
            if (infectionBar != null)
            {
                infectionBar.value = value;
            }
        }

        private void UpdateTemperatureBar(float value)
        {
            if (temperatureBar != null)
            {
                temperatureBar.value = value;
            }
        }

        public void UpdateTimeText(string time)
        {
            if (timeText != null)
            {
                timeText.text = time;
            }
        }

        public void UpdateTemperatureText(string temperature)
        {
            if (temperatureText != null)
            {
                temperatureText.text = temperature;
            }
        }

        public void ShowDisasterWarning(string warning)
        {
            if (disasterWarningText != null)
            {
                disasterWarningText.text = warning;
                disasterWarningText.gameObject.SetActive(true);
            }
        }

        public void HideDisasterWarning()
        {
            if (disasterWarningText != null)
            {
                disasterWarningText.gameObject.SetActive(false);
            }
        }
    }
} 